<template>
  <div class="tags-add">
    <el-dialog :title="text+pageTitle" :visible.sync="dialogFormVisible" width="400px" @close="handleClose">
      <el-form label-width="80px" ref="tagsForm" :model="tagsForm" :rules="tagsFormRule">
        <el-form-item label="所属学科" prop="subjectID" size="small" v-if="isShow">
          <el-select v-model="tagsForm.subjectID" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in subjects"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName" size="small">
          <el-input v-model="tagsForm.tagName" placeholder="请输入标签名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button @click="handleClose">{{$t('table.cancel')}}</el-button>
          <el-button type="primary" @click="onSave">{{$t('table.confirm')}}</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags.js'
export default {
  data () {
    return {
      dialogFormVisible: false,
      tagsForm: {
        subjectID: '',
        tagName: ''
      },
      tagsFormRule: {
        subjectID: [{ required: true, message: '请选择所属学科', trigger: 'blur' }],
        tagName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }]
      }
    }
  },
  props: {
    // 创建or修改
    text: {
      type: String
    },
    // 目录or标签
    pageTitle: {
      type: String
    },
    subjects: {
      type: Array,
      required: true
    },
    isShow: {
      type: Boolean,
      required: true
    }
  },
  methods: {
    // 弹层显示
    dialogFormV (row) {
      if (row) this.tagsForm = { ...this.tagsForm, ...row }
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH () {
      this.dialogFormVisible = false
    },
    // 退出
    handleClose () {
      this.dialogFormVisible = false
      this.$refs.tagsForm.resetFields()
    },
    // 点击确认
    async onSave () {
      try {
        await this.$refs.tagsForm.validate()
        if (this.tagsForm.id) {
          // 修改
          await update(this.tagsForm)
          this.$message.success('修改标签成功')
        } else {
          // 新增
          await add(this.tagsForm)
          this.$message.success('新增标签成功')
        }
        this.$emit('success')
        this.handleClose()
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
